<?php
if ( !defined('IN_ADMINCP') || !defined('IN_SEC') ){
	exit('Access Denied.');
}
if( $_G['gp_mode'] == 'detail' ){
	$companys = get_parse_companys();
	$datas = account_parse_companys($companys);
	$tmpname = '_detail';
}else{
	#==========Search
	$sea_high = $_G['gp_sea_high'];
	$sea_company = intval($_G['gp_company']);
	$sea_department = intval($_G['gp_department']);

	$where = array();
	if( $_G['gp_manage'] ){
		$groups = array();
		foreach( $admincp->groups as $k => $v ){
			if( $v['id'] == 1 || $v['rank'] >= 80 ){
				$groups[$k] = $v;
			}
		}
		$ids = idserialize($groups);
		$where[] = "`groupid` IN('{$ids}')";
	}

	if( $sea_company ){
		$where['companyid'] = $sea_company;
	}
	if( $sea_department ){
		$where['departmentid'] = $sea_department;
	}


	$admins = limitquery('admincp_member', $where);
	$departments = parse_array(limitquery('admincp_department'));
	$companys = parse_array(limitquery('companys'));
}

function account_parse_companys($companys = array(), $name = 'company', $type = 'folder'){
	global $admincp;
	$datas = array();
	foreach( $companys as $k => $v ){
		$c = $d = $com = array();
		$key = $name . '-' . $v['id'];
		$datas[$key] = array(
			'name' => $v['name'],
			'type' => $type
		);

		if( $type == 'item' ){
			$datas[$key]['name'] = '[' . $admincp->groups[$v['groupid']]['name'] . ']' . ($v['realname'] ? $v['realname'] : $v['username']);
		}

		if( $v['users'] ){
			$c = account_parse_companys($v['users'], 'users', 'item');
		}
		if( $v['sub_department'] ){
			$d = account_parse_companys($v['sub_department'], 'department');
		}
		if( $v['sub_company'] ){
			$com = account_parse_companys($v['sub_company']);
		}
		foreach( $c as $ck => $cv ){
			$datas[$key]['additionalParameters']['children'][] = $cv;
		}
		foreach( $d as $dk => $dv ){
			$datas[$key]['additionalParameters']['children'][] = $dv;
		}
		foreach( $com as $comk => $comv ){
			$datas[$key]['additionalParameters']['children'][] = $comv;
		}
	}
	return $datas;
}

?>